package com.neuedu.mvc.dao;

import com.neuedu.dbutils.JDBCUtils;
import com.neuedu.mvc.entity.Department;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-18 11:40
 * 描述 :
 */
public class DepartmentDao {


    /**
     * 查询集合
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public List<Department> selectList(String sql , Object ... params) throws SQLException, ClassNotFoundException {
        Connection conn = JDBCUtils.getConnection();
        return JDBCUtils.selectList(conn, Department.class  , sql,params);
    }

    /**
     * 分页访问查询结合
     * @param sql
     * @param current
     * @param size
     * @param params
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public List<Department> selectPage(String sql ,  int current,int size , Object ... params) throws SQLException, ClassNotFoundException {
        Connection conn = JDBCUtils.getConnection();
        return JDBCUtils.selectPage(conn, Department.class  , sql,  current, size, params);
    }

    /**
     * 根据主键查询一个
     * @param id
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public Department selectOne(int id) throws SQLException, ClassNotFoundException {
        Connection conn = JDBCUtils.getConnection();
        String sql =" select * from department where id = ?";
        return JDBCUtils.selectOne(conn, Department.class  , sql,id);
    }

    public long count(String sql ,Object ... params) throws SQLException, ClassNotFoundException {
        Connection conn = JDBCUtils.getConnection();
        return JDBCUtils.selectTotal(conn, sql , params);
    }


    /**
     * 插入
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public int insert(String sql , Object ... params) throws SQLException, ClassNotFoundException {
        Connection conn = JDBCUtils.getConnection();
        return JDBCUtils.update(conn,  sql,params);
    }

    /**
     * 更新
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public int update(String sql , Object ... params) throws SQLException, ClassNotFoundException {
        Connection conn = JDBCUtils.getConnection();
        return JDBCUtils.update(conn,  sql,params);
    }


    /**
     *  根据主键删除
     * @param id
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public int deleteById(int id) throws SQLException, ClassNotFoundException {
        Connection conn = JDBCUtils.getConnection();
        String sql ="delete from department where id = ?";
        return JDBCUtils.update(conn,  sql,id);
    }



}
